package com.project.service.article;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.common.Constants;
import com.project.dao.article.ArticleDao;
import com.project.entity.article.Article;
import com.project.service.BaseService;
import com.project.util.CurrentSessionUtil;
import com.project.util.GenerateCode;
import com.project.util.qrcode.QrcodeUtil;

@Service
@Transactional
public class ArticleService extends BaseService<Article>{
	
	@Autowired
	private ArticleDao articleDao;
	
	@Override
	public void save(Article entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setCreateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
        articleDao.save(entity);
        //生成二维码
        String url =  QrcodeUtil.createLocalQrcode(500, 500,Constants.CONTEXT_PATH+"#/ArticalDetail?id="+entity.getId(),"article_qrcode"+GenerateCode.randomCode(4));
        entity.setQrcodeUrl("/"+url);
        articleDao.update(entity);
        
    }
	
	@Override
	public void update(Article entity) {
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
        articleDao.update(entity);
    }
	
}
